package com.javaweb.admin.config;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.javaweb.admin.entity.KdlPurchaseinReturn;
import com.javaweb.admin.entity.NcTaskLog;
import com.javaweb.admin.entity.PurchaseinInspectionResult;
import com.javaweb.admin.entity.Scheduled;
import com.javaweb.admin.mapper.KdlPurchaseinReturnMapper;
import com.javaweb.admin.mapper.ScheduledMapper;
import com.javaweb.admin.query.KdlPurchaseinQuery;
import com.javaweb.admin.query.KdlPurchaseinReturnQuery;
import com.javaweb.admin.service.AbstractJob;
import com.javaweb.admin.service.IKdlPurchaseinReturnService;
import com.javaweb.admin.service.IKdlPurchaseinService;
import com.javaweb.admin.service.INcTaskLogService;
import com.javaweb.common.config.CommonConfig;
import com.javaweb.common.utils.DateUtils;
import com.javaweb.common.utils.JsonResult;
import com.seegw.shaphar.IKDLService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;
@Component
public class SyncKdlPurchaseReturn2NC extends AbstractJob {
    private static final Logger log = LoggerFactory.getLogger(SyncKdlPurchase2NC.class);
    @Autowired
    ScheduledMapper scheduledMapper;

    @Autowired
    IKDLService kdlService;

    @Autowired
    IKdlPurchaseinReturnService kdlPurchaseinReturnService;

    @Autowired
    INcTaskLogService iNcTaskLogService;

    @Autowired
    private KdlPurchaseinReturnMapper kdlPurchaseinReturnMapper;

    @Override
    public String getName() {
        QueryWrapper<Scheduled> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("classpath",this.getClass().getName());
        Scheduled entity = scheduledMapper.selectOne(queryWrapper);
        return entity.getName();
    }

    @Override
    public void execute() {
        //获取康德乐采购入库质检回传
        KdlPurchaseinReturnQuery entity = new KdlPurchaseinReturnQuery();
        entity.setPrincipalid(CommonConfig.kdlCompanyId);
        entity.setDoctype("ZST3");
        entity.setStarttime(kdlPurchaseinReturnService.getMaxSyncTime());
        entity.setEndtime(DateUtils.getTime());
        try {
            log.info("开始获取康德乐采购退货回传....");
            kdlPurchaseinReturnService.syncPurchaseReturn(entity);
            log.info("获取康德乐采购退货回传完成");
        }
        catch(Exception ex){
            log.error(ex.getMessage());
        }
        //获取未完成上传的采退记录
        List<KdlPurchaseinReturn> returnList = kdlPurchaseinReturnService.getPurchaseReturnSubmitList();
        NcTaskLog taskLog = new NcTaskLog();
        taskLog.setTaskType("采退回传NC");
        taskLog.setBillCount(returnList.size());
        taskLog.setStartTime(DateUtils.now());
        String billno = "";

        log.info(String.format("开始回写NC采退回传,获取到%d个待回写订单....",returnList.size()));
        JsonResult ret;
        for (KdlPurchaseinReturn result: returnList) {
            try {
                ret = kdlService.submitPurchaseReturnToNC(result.getId());
                if(ret.getCode()==0){
                    log.info("采退订单:"+result.getVendorpo()+"回写NC成功");
                }
                else {
                    log.info("采退订单:"+result.getVendorpo()+"回写NC失败，错误:"+ret.getMsg());
                }
                billno+=result.getVendorpo()+",";
            }catch(Exception ex)
            {
                log.error(ex.getMessage());
            }
        }
        taskLog.setBillNo(billno);
        taskLog.setEndTime(DateUtils.now());
        iNcTaskLogService.save(taskLog);


    }
}
